package tron;

import java.net.*;
import java.io.*;

public class Client {
    int timeout = 5000;
    ObjectInputStream in = null;
    ObjectOutputStream out = null;
    Socket socket;
    Game game;
    
    public Client(Game game, Field field, FieldPanel fieldPanel) {
	this.game = game;
	int serverPort = 6666;
	String address = "127.0.0.1";
	try {
	    InetAddress ipAddress = InetAddress.getByName(address);
	    socket = new Socket(ipAddress, serverPort);
	    
	} catch (Exception x) {
	    x.printStackTrace();
	}
    }

    public void startClient() {
	try {
	    out = new ObjectOutputStream(socket.getOutputStream());
	    in = new ObjectInputStream(socket.getInputStream());
	    while (true) {
		//Snake snake = new Snake(1, 2, 10, Snake.Direction.DOWN);
		Snake snake = game.field.getSnake(game.numOfSnake);
		System.out.println("Client " + snake);
		out.writeObject(snake);
		out.flush();
		
		
		/*Field field = (Field) in.readObject();
		game.setField(field);
		game.fieldPanel.repaint();
		int winner = field.winner();
		if (winner != -1) {
		    if (winner == 0)
			System.out.println("Draw");
		    else
			System.out.println("Winner " + winner);
		    return;
		} */
		Thread.sleep(timeout );
	    }

	} catch (Exception x) {
	    x.printStackTrace();
	    System.exit(1);
	}
    }
}